package com.tongda.menu.adapter;

import java.math.BigDecimal;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.tongda.core.enums.DishesStatusEnum;
import com.tongda.core.repository.MenuCartDao;
import com.tongda.core.repository.MenuDishCategoryDAO;
import com.tongda.core.repository.MenuDishDao;
import com.tongda.core.service.BillService;
import com.tongda.core.utils.ActivityUtil;
import com.tongda.core.vo.Dish;
import com.tongda.core.vo.DishesSetDishes;
import com.tongda.menu.R;
import com.tongda.menu.R.color;
import com.tongda.menu.R.drawable;
import com.tongda.menu.activity.CartActivity;
import com.tongda.menu.activity.MainActivity;
import com.tongda.menu.activity.OpenTableActivity;
import com.tongda.menu.activity.ShowActivity;
import com.tongda.menu.activity.CartActivity.BillurgeTask;
import com.tongda.menu.activity.CartActivity.BillurgeTimerTask;
import com.tongda.menu.activity.CartActivity.OrderTask;
import com.tongda.menu.activity.CartEditActivity;
import com.tongda.menu.util.ActivityMenuUtil;

public class OrderedDishAdapter extends BaseAdapter{
	
	List<Dish> datas;
	Context context;
	private static int AdapterCuiCaiTimer = 30;
	
	Handler billUrgeHandler = new Handler();
	Runnable billUrgeRunnable = new Runnable() {
		
		@Override
		public void run() {
			new AdapterBillurgeTimerTask().execute(AdapterCuiCaiTimer);
			billUrgeHandler.postDelayed(billUrgeRunnable, 10000);
		}
	};
	
	public OrderedDishAdapter (List<Dish> datas,Context context){
		this.datas = datas;
		this.context = context;
	}
	
	
	@Override
	public int getCount() {
		return datas.size();
	}

	
	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Holder holder;
		if(convertView==null){
			holder = new Holder();
			convertView= View.inflate(context, R.layout.ordereddishitem, null);
//			holder.tuicaibg = (LinearLayout)convertView.findViewById(R.id.tuicaibg);
			holder.dishNameTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishname);
			holder.dishNoteTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishnote);
			holder.dishCategoryTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishcategory);
			holder.dishsPriceTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishsPrice);
			holder.dishpriceTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishprice);
			holder.dishNumberTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishnumber);
			holder.dishnumnameTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishnumname);
			holder.dishStateTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishstate);
			holder.dishTotalTv = (TextView)convertView.findViewById(R.id.adapter_item_ordereddishtotal);
			holder.dishEditBtn = (ImageView)convertView.findViewById(R.id.adapter_item_ordereddishedit);
			holder.dishDeleteBtn = (ImageView)convertView.findViewById(R.id.adapter_item_ordereddishdelete);
			holder.dishminusImv = (ImageView)convertView.findViewById(R.id.adapter_item_orderedminus);
			holder.dishplusImv = (ImageView)convertView.findViewById(R.id.adapter_item_orderedplus);
//			holder.dishrmbTv = (TextView)convertView.findViewById(R.id.adapter_item_renminbi);
			
			holder.IconzhaopaiImv = (ImageView)convertView.findViewById(R.id.adapter_cartitem_icon_zhaopai);
			holder.IcontejiaImv = (ImageView)convertView.findViewById(R.id.adapter_cartitem_icon_tejia);
			holder.IcontuijianImv = (ImageView)convertView.findViewById(R.id.adapter_cartitem_icon_tuijian);
			holder.IconshijiaImv = (ImageView)convertView.findViewById(R.id.adapter_cartitem_icon_shijia);
			
			holder.dsDisheLy = (TextView)convertView.findViewById(R.id.adapter_item_dsdishes);
			
			holder.yuanTv = (TextView)convertView.findViewById(R.id.adapter_item_yuan);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		MenuDishCategoryDAO dishcategoryDao = MenuDishCategoryDAO.getInstance(context);
		final MenuDishDao dish_dao = MenuDishDao.getInstance(context);
		final MenuCartDao order_dish_dao = MenuCartDao.getInstance(context);
		final Dish dish = datas.get(position);
		
		holder.dishNameTv.setText(dish.getDishesName());
		
	    /*
         * 处理菜肴备注信息
         * */
		StringBuilder sb = new StringBuilder("");
		String avoidNameStr;
		String tasteNameStr;
		String laduStr;
		String billNotesStr;
		String[] ladu_showArr = new String[] { "默认", "不辣", "微辣", "中辣", "辣", "特辣" };
		if(dish.getAvoidfoodNameArray()==null || dish.getAvoidfoodNameArray().equals("null")){
			avoidNameStr = "";
		}else{
			avoidNameStr = dish.getAvoidfoodNameArray();
		}
		
		if(dish.getTasteNameArray()==null || dish.getTasteNameArray().equals("null")){
			tasteNameStr = "";
		}else{
			tasteNameStr = dish.getTasteNameArray();
		}
		
		if(dish.getLadu_value()==0){
			laduStr = "";
		}else{
			laduStr = ladu_showArr[Integer.valueOf(dish.getLadu_value())];
		}
		
		if(dish.getBillnotes()==null || dish.getBillnotes().equals("")){
			billNotesStr = "";
		}else{
			billNotesStr = dish.getBillnotes();
		}
		sb.append("烹饪备注：").append(avoidNameStr).append(tasteNameStr).append(laduStr).append(billNotesStr);
		holder.dishNoteTv.setText(sb.toString());
        /*
         * 处理价格
         * */
		if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
			holder.dishsPriceTv.setVisibility(View.INVISIBLE);
        	holder.dishpriceTv.setText("￥"+dish.getRulingPrice()+"/"+dish.getUnitName());
        	holder.dishpriceTv.getPaint().setFlags(0);
		}else{
        if(dish.getSpecialPrice()!=null && !dish.getSpecialPrice().equals(dish.getPrice()) && ((CartActivity)(context)).canSpecialPrice==true){
        	holder.dishsPriceTv.setVisibility(View.VISIBLE);
        	holder.dishsPriceTv.setText(Html.fromHtml("<font color='black' type='bold'>"+"￥"+dish.getSpecialPrice()+"/"+dish.getUnitName()+"</font>"));
        	holder.dishpriceTv.setText("￥"+dish.getPrice());
        	holder.dishpriceTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }else{
        	holder.dishsPriceTv.setVisibility(View.INVISIBLE);
        	holder.dishpriceTv.setText("￥"+dish.getPrice()+"/"+dish.getUnitName());
        	holder.dishpriceTv.getPaint().setFlags(0);
        }
		}
		
		float num  = dish.getOrdersalenum();
		
		holder.dishNumberTv.setText(String.valueOf(num));
		holder.dishnumnameTv.setText(dish.getUnitName());
		
		String dishcategoryName = dishcategoryDao.getDishCategoryName(dish.getCategoryId());
		holder.dishCategoryTv.setText(dishcategoryName);
        /*
         * 处理总价
         * */
        
        float numInt = Float.valueOf(holder.dishNumberTv.getText().toString());
        BigDecimal numBd = BigDecimal.valueOf(numInt);
        if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
        	 holder.dishTotalTv.setText((new BigDecimal(dish.getRulingPrice()).multiply(numBd)).toString());
        }else{
        if(dish.getSpecialPrice()!=null&& !dish.getSpecialPrice().equals(dish.getPrice()) && ((CartActivity)(context)).canSpecialPrice==true){
        	  holder.dishTotalTv.setText((dish.getSpecialPrice().multiply(numBd)).toString());
        }else{
        	  holder.dishTotalTv.setText((dish.getPrice().multiply(numBd)).toString());
        }
        }
		//处理减少数量
		holder.dishminusImv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				float num = Float.valueOf(holder.dishNumberTv.getText().toString());
				if(num>1){
				--num;
				holder.dishNumberTv.setText(String.valueOf(num));
				dish.setOrdersalenum(num);
				
				if(dish.getIsDinerDish().equals("0")){
					order_dish_dao.updateOrdishesNum(String.valueOf(dish.get_id()), String.valueOf(num));
					dish_dao.minusNum(dish.getDishesId());
				}else if(dish.getIsDinerDish().equals("1")){
					//账单菜肴走账单的减菜方法
					((CartActivity) context).changeDinerBillDishNum(dish.getDinerBillDisheId(), String.valueOf(num), dish.getIsSet());
				}
				
		        float numInt = Float.valueOf(holder.dishNumberTv.getText().toString());
		        BigDecimal numBd = BigDecimal.valueOf(numInt);
		        if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
		        	 holder.dishTotalTv.setText((new BigDecimal(dish.getRulingPrice()).multiply(numBd)).toString());
		        }else{
		        if(dish.getSpecialPrice()!=null){
		        	  holder.dishTotalTv.setText((dish.getSpecialPrice().multiply(numBd)).toString());
		        }else{
		        	  holder.dishTotalTv.setText((dish.getPrice().multiply(numBd)).toString());
		        }
		        }
		        
		       float totalNum = ( (CartActivity)(context)).totalNum;
		       totalNum--;
		       ( (CartActivity)(context)).totalNum = totalNum;
		       ( (CartActivity)(context)).billNumStr.setText(String.valueOf("共"+totalNum+"份"));
		        
		       BigDecimal totalPrice;
		       if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
		    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(new BigDecimal(dish.getRulingPrice()));
		       }else{
		       if(dish.getSpecialPrice()!=null){
		    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(dish.getSpecialPrice());
		       }else{
		    	   totalPrice = (((CartActivity)(context)).totalPrice).subtract(dish.getPrice());
		       }
		       }
		       
		       ((CartActivity)(context)).setTotalPrice(totalPrice);
		       ((CartActivity)(context)).dishestotalPriceTv.setText(totalPrice.toString());
		       
		       ((CartActivity)(context)).billtotalMoney.setText((totalPrice.add(((CartActivity)(context)).billServiceMoney)).toString());
		       
				}else{
					//执行删除操作
					
			        Dialog dialog = new AlertDialog.Builder(context). 
			                setTitle("您确定删除该菜肴吗？"). 
			                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
			                     
			                    @Override 
			                    public void onClick(DialogInterface dialog, int which) { 
			    			        
			                    	if(dish.getIsDinerDish().equals("0")){
			                    		 dish_dao.updateMinusNum(dish.getDishesId(), Float.valueOf(holder.dishNumberTv.getText().toString()));
			                    	     order_dish_dao.deleteOrderDish(dish.get_id());
			                    	}else{
			                    		String isSet = null;
			        					if(dish.getIsSet().equals("false")){
			        						isSet="0";
			        					}else if(dish.getIsSet().equals("true")){
			        						isSet = "1";
			        					}
			                    	     ( (CartActivity)(context)).shancai(dish.getDinerBillDisheId(), isSet);
			                    	}
			    				    
			    				    float totalNum = ( (CartActivity)(context)).totalNum;
			    				    float orderNum = dish.getOrdersalenum();
			    				       totalNum =totalNum-orderNum;
			    				       ( (CartActivity)(context)).totalNum = totalNum;
			    				       ( (CartActivity)(context)).billNumStr.setText(String.valueOf("共"+totalNum+"份"));
			    				       
			    				       BigDecimal totalPrice;
			    				       if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
			    				    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(new BigDecimal(dish.getRulingPrice()).multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }else{
			    				       if(dish.getSpecialPrice()!=null){
			    				    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(dish.getSpecialPrice().multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }else{
			    				    	   totalPrice = (((CartActivity)(context)).totalPrice).subtract(dish.getPrice().multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }
			    				       }
			    				       
			    				       ((CartActivity)(context)).setTotalPrice(totalPrice);
			    				       ((CartActivity)(context)).dishestotalPriceTv.setText(totalPrice.toString());      
			    				       
			    				       ((CartActivity)(context)).billtotalMoney.setText((totalPrice.add(((CartActivity)(context)).billServiceMoney)).toString());
			    				       
			    						datas.remove(dish);
			    						notifyDataSetChanged();
			                    } 
			                }). 
			                setNegativeButton("取消", new DialogInterface.OnClickListener() {
			                     
			                    @Override 
			                    public void onClick(DialogInterface dialog, int which) { 
			                    	dialog.dismiss();
			                    } 
			                }). 
			                create(); 
			        dialog.show();
				}
			}
		});
		//处理增加数量
		holder.dishplusImv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				float num = Float.valueOf(holder.dishNumberTv.getText().toString());
				++num;
				holder.dishNumberTv.setText(String.valueOf(num));
				dish.setOrdersalenum(num);
				if(dish.getIsDinerDish().equals("0")){
					order_dish_dao.updateOrdishesNum(String.valueOf(dish.get_id()), String.valueOf(num));
					dish_dao.addNum(dish.getDishesId());
				}else if(dish.getIsDinerDish().equals("1")){
					((CartActivity) context).changeDinerBillDishNum(dish.getDinerBillDisheId(), String.valueOf(num), dish.getIsSet());
				}
				
		        float numInt = Float.valueOf(holder.dishNumberTv.getText().toString());
		        BigDecimal numBd = BigDecimal.valueOf(numInt);
		        if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
		        	holder.dishTotalTv.setText((new BigDecimal(dish.getRulingPrice()).multiply(numBd)).toString());
		        }else{
		        if(dish.getSpecialPrice()!=null){
		        	  holder.dishTotalTv.setText((dish.getSpecialPrice().multiply(numBd)).toString());
		        }else{
		        	  holder.dishTotalTv.setText((dish.getPrice().multiply(numBd)).toString());
		        }
		        }
			       float totalNum = ( (CartActivity)(context)).totalNum;
			       totalNum++;
			       ( (CartActivity)(context)).totalNum = totalNum;
		           ((CartActivity)(context)).billNumStr.setText(String.valueOf("共"+totalNum+"份"));
		           
			       BigDecimal totalPrice;
			       if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
			    	   totalPrice =  (((CartActivity)(context)).totalPrice).add(new BigDecimal(dish.getRulingPrice()));
			       }else{
			       if(dish.getSpecialPrice()!=null){
			    	   totalPrice =  (((CartActivity)(context)).totalPrice).add(dish.getSpecialPrice());
			       }else{
			    	   totalPrice = (((CartActivity)(context)).totalPrice).add(dish.getPrice());
			       }
			       }
			       ((CartActivity)(context)).setTotalPrice(totalPrice);
			       ((CartActivity)(context)).dishestotalPriceTv.setText(totalPrice.toString());
			       
			       ((CartActivity)(context)).billtotalMoney.setText((totalPrice.add(((CartActivity)(context)).billServiceMoney)).toString());
			       
			}
		});
		
		
		//处理购物车中每道菜的下单状态
		String dishStatus = dish.getDishesStatus();
		if(dishStatus.equals("0")){
			holder.dishpriceTv.setTextColor(Color.BLACK);
			holder.dishsPriceTv.setTextColor(color.red);
//			holder.tuicaibg.setBackgroundResource(0);
			holder.dishStateTv.setText("未下单");
			holder.dishStateTv.setTextColor(context.getResources().getColor(R.color.black));
//			holder.dishrmbTv.setTextColor(context.getResources().getColor(R.color.red));
			holder.dishplusImv.setEnabled(true);
			holder.dishminusImv.setEnabled(true);
			holder.dishplusImv.setImageResource(drawable.plus);
			holder.dishminusImv.setImageResource(drawable.minus);
		     //处理编辑按钮
			holder.dishEditBtn.setVisibility(View.VISIBLE);
			holder.dishEditBtn.setBackgroundResource(R.drawable.cart_item_edit);
			holder.dishEditBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(ActivityMenuUtil.checkNetworkState(context)){
						((CartActivity)(context)).showCartEditActivity("dish",dish.getDishesId(),dish);
					}else{
						ActivityUtil.Toast(((CartActivity)(context)), "请开启设备网络环境", false, false);
					}
				}
			});
			//处理删除按钮
			holder.dishDeleteBtn.setVisibility(View.VISIBLE);
			holder.dishDeleteBtn.setBackgroundResource(R.drawable.cart_item_delete);
			holder.dishDeleteBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
			        Dialog dialog = new AlertDialog.Builder(context). 
			                setTitle("您确定删除该菜肴吗？"). 
			                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
			                     
			                    @Override 
			                    public void onClick(DialogInterface dialog, int which) { 
			                    	if(dish.getIsDinerDish().equals("0")){
			                    		 dish_dao.updateMinusNum(dish.getDishesId(), Float.valueOf(holder.dishNumberTv.getText().toString()));
			                    	     order_dish_dao.deleteOrderDish(dish.get_id());
			                    	}else{
			                    		String isSet = null;
			        					if(dish.getIsSet().equals("false")){
			        						isSet="0";
			        					}else if(dish.getIsSet().equals("true")){
			        						isSet = "1";
			        					}
			                    	     ( (CartActivity)(context)).shancai(dish.getDinerBillDisheId(), isSet);
			                    	}
			                    	
			    				    
//			    				    float totalNum = Float.valueOf(( (CartActivity)(context)).billNumStr.getText().toString());
			                        float totalNum = ( (CartActivity)(context)).totalNum;
			    				    float orderNum = dish.getOrdersalenum();
			    				       totalNum =totalNum-orderNum;
			    				       ( (CartActivity)(context)).totalNum = totalNum;
			    				       ( (CartActivity)(context)).billNumStr.setText(String.valueOf("共"+totalNum+"份"));
			    				       
			    				       BigDecimal totalPrice;
			    				       if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
			    				    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(new BigDecimal(dish.getRulingPrice()).multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }else{
			    				       if(dish.getSpecialPrice()!=null){
			    				    	   totalPrice =  (((CartActivity)(context)).totalPrice).subtract(dish.getSpecialPrice().multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }else{
			    				    	   totalPrice = (((CartActivity)(context)).totalPrice).subtract(dish.getPrice().multiply(BigDecimal.valueOf(dish.getOrdersalenum())));
			    				       }
			    				       }
			    				       ((CartActivity)(context)).setTotalPrice(totalPrice);
			    				       ((CartActivity)(context)).dishestotalPriceTv.setText(totalPrice.toString());      
			    				       
			    				       ((CartActivity)(context)).billtotalMoney.setText((totalPrice.add(((CartActivity)(context)).billServiceMoney)).toString());
			    				       
			    						datas.remove(dish);
			    						notifyDataSetChanged();
			                    } 
			                }). 
			                setNegativeButton("取消", new DialogInterface.OnClickListener() {
			                     
			                    @Override 
			                    public void onClick(DialogInterface dialog, int which) { 
			                    	dialog.dismiss();
			                    } 
			                }). 
			                create(); 
			        dialog.show();
				}
			});
			holder.dishNameTv.setTextColor(context.getResources().getColor(R.color.cart_dish_light_green));
			holder.dishCategoryTv.setTextColor(Color.BLACK);
			holder.dishStateTv.setTextColor(Color.BLACK);
			holder.dishTotalTv.setTextColor(Color.RED);
			holder.yuanTv.setTextColor(Color.RED);
			holder.dishNumberTv.setTextColor(context.getResources().getColor(R.color.black));
			holder.dishnumnameTv.setTextColor(context.getResources().getColor(R.color.black));
		}else {
			if(dishStatus.equals("1")){
				holder.dishpriceTv.setTextColor(Color.BLACK);
				holder.dishsPriceTv.setTextColor(color.red);
//				holder.tuicaibg.setBackgroundResource(0);
				holder.dishStateTv.setText("已下单");
				holder.dishStateTv.setTextColor(context.getResources().getColor(R.color.red));
//				holder.dishrmbTv.setTextColor(context.getResources().getColor(R.color.red));
				holder.dishplusImv.setEnabled(false);
				holder.dishminusImv.setEnabled(false);
				holder.dishplusImv.setImageResource(drawable.plus_unclick);
				holder.dishminusImv.setImageResource(drawable.minus_unclick);
				holder.dishEditBtn.setVisibility(View.VISIBLE);
				holder.dishEditBtn.setBackgroundResource(R.drawable.cart_item_call);
				holder.dishEditBtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						--AdapterCuiCaiTimer;
						billUrgeHandler.postDelayed(billUrgeRunnable,10000);
						if(AdapterCuiCaiTimer>=29){
							new DishurgeTask().execute(dish);
//							((CartActivity)(context)).Toast("单菜催菜成功!");
						}else{
							((CartActivity)(context)).Toast("您刚刚催过菜了,请稍等......");
						}
					}
				});
				
				holder.dishDeleteBtn.setVisibility(View.VISIBLE);
				holder.dishDeleteBtn.setBackgroundResource(R.drawable.cart_item_back);
				holder.dishDeleteBtn.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(dish!=null){
							((CartActivity)(context)).showVerifyId("tuicai",dish);
						}else{
							Toast.makeText(context, "没有获取到菜肴信息,请点击重新获取", Toast.LENGTH_SHORT).show();
						}
						
					}
				});
				holder.dishNameTv.setTextColor(context.getResources().getColor(R.color.cart_dish_light_green));
				holder.dishCategoryTv.setTextColor(Color.BLACK);
				holder.dishStateTv.setTextColor(Color.RED);
				holder.dishTotalTv.setTextColor(Color.RED);
				holder.yuanTv.setTextColor(Color.RED);
				holder.dishNumberTv.setTextColor(context.getResources().getColor(R.color.black));
				holder.dishnumnameTv.setTextColor(context.getResources().getColor(R.color.black));
			}
			else if(DishesStatusEnum.SERVED.getCode().equals(dishStatus)){
//				holder.tuicaibg.setBackgroundResource(drawable.tuicaibg);
				holder.dishStateTv.setText("已上菜");
				holder.dishStateTv.setTextColor(context.getResources().getColor(R.color.gray));
//				holder.dishrmbTv.setTextColor(context.getResources().getColor(R.color.gray));
				holder.dishEditBtn.setVisibility(View.INVISIBLE);
				holder.dishDeleteBtn.setVisibility(View.INVISIBLE); 
				holder.dishplusImv.setEnabled(false);
				holder.dishminusImv.setEnabled(false);
				
				holder.dishNameTv.setTextColor(color.gray);
				holder.dishNoteTv.setTextColor(color.gray);
				holder.dishCategoryTv.setTextColor(color.gray);
				holder.dishpriceTv.setTextColor(color.gray);
				holder.dishsPriceTv.setTextColor(color.gray);
				holder.dishStateTv.setTextColor(color.gray);
				holder.dishTotalTv.setTextColor(color.gray);
				holder.yuanTv.setTextColor(color.gray);
				holder.dishNumberTv.setTextColor(color.gray);
				holder.dishnumnameTv.setTextColor(color.gray);
				
				holder.dishplusImv.setImageResource(drawable.plus_unclick);
				holder.dishminusImv.setImageResource(drawable.minus_unclick);
			}
			else if(DishesStatusEnum.UNSERVE_CANCEL.getCode().equals(dishStatus)
					|| DishesStatusEnum.SERVED_CANCEL.getCode().equals(dishStatus)){
//				holder.tuicaibg.setBackgroundResource(drawable.tuicaibg);
				holder.dishStateTv.setText("已退菜");
				holder.dishStateTv.setTextColor(context.getResources().getColor(R.color.gray));
//				holder.dishrmbTv.setTextColor(context.getResources().getColor(R.color.gray));
				holder.dishEditBtn.setVisibility(View.INVISIBLE);
				holder.dishDeleteBtn.setVisibility(View.INVISIBLE); 
				holder.dishplusImv.setEnabled(false);
				holder.dishminusImv.setEnabled(false);
				
				holder.dishNameTv.setTextColor(color.gray);
				holder.dishNoteTv.setTextColor(color.gray);
				holder.dishCategoryTv.setTextColor(color.gray);
				holder.dishpriceTv.setTextColor(color.gray);
				holder.dishsPriceTv.setTextColor(color.gray);
				holder.dishStateTv.setTextColor(color.gray);
				holder.dishTotalTv.setTextColor(color.gray);
				holder.yuanTv.setTextColor(color.gray);
				holder.dishNumberTv.setTextColor(color.gray);
				holder.dishnumnameTv.setTextColor(color.gray);
				
				holder.dishplusImv.setImageResource(drawable.plus_unclick);
				holder.dishminusImv.setImageResource(drawable.minus_unclick);
			}
		}
        /*
         * 处理图标
         * */
        if(dish.getIsSpecialty()!=null &&  !dish.getIsSpecialty().equals("null")){
        	holder.IconzhaopaiImv .setVisibility(View.VISIBLE);
        	holder.IconzhaopaiImv .setImageResource(drawable.zhaopai_small);
        }else{
        	holder.IconzhaopaiImv .setVisibility(View.GONE);
        }
        
        if(dish.getIsRecommend()!=null && !dish.getIsRecommend().equals("null")){
        	holder.IcontuijianImv .setVisibility(View.VISIBLE);
        	holder.IcontuijianImv .setImageResource(drawable.tuijian_small);
        }else{
        	holder.IcontuijianImv .setVisibility(View.GONE);
        }
        
        if((dish.getSpecialPrice()!=null && !dish.getSpecialPrice().equals(dish.getPrice()))|| dish.getMemberPrice()!=null){
        	holder.IcontejiaImv .setVisibility(View.VISIBLE);
        	holder.IcontejiaImv .setImageResource(drawable.tejia_small);
        }else{
        	holder.IcontejiaImv .setVisibility(View.GONE);
        }
        
        if(dish.getIsRulingPrice()!=null &&dish.getIsRulingPrice().equals("1")){
        	holder.IconshijiaImv .setVisibility(View.VISIBLE);
        	holder.IconshijiaImv .setImageResource(drawable.shijia_small);
        }else{
        	holder.IconshijiaImv .setVisibility(View.GONE);
        }
        
        holder.dishNameTv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((CartActivity)context).showMenuShowActivity(dish.getDishesName());
			}
		});
        
//	    /*
//         * 如果是套餐则显示其对应的子菜肴
 //        * 
        StringBuilder dsSb = new StringBuilder("");
        if(dish.getIsSet()!=null && dish.getIsSet().equals("true") && dish.getDsDishes()!=null){
        	holder.dsDisheLy.setVisibility(View.VISIBLE);
        	for(DishesSetDishes ds : dish.getDsDishes()){
        		dsSb.append(ds.getDishesName()+"x"+ds.getUnitNum()+"  ");
        	}
    		holder.dsDisheLy.setText(dsSb.toString());
        }else{
        	holder.dsDisheLy.setVisibility(View.GONE);
        	dsSb = new StringBuilder("");
        }
        
		return convertView;
	}
	class Holder {
		LinearLayout tuicaibg;
		TextView dishNameTv;
		TextView dishNoteTv;
		TextView dishCategoryTv;
		TextView dishpriceTv;
		TextView dishsPriceTv;
		TextView dishNumberTv;
		TextView dishnumnameTv;
		TextView dishStateTv;
		TextView dishTotalTv;
		ImageView dishEditBtn;
		ImageView dishDeleteBtn;
		ImageView dishminusImv;
		ImageView dishplusImv;
		TextView dishrmbTv;
		
		 ImageView IconzhaopaiImv;
		 ImageView IcontuijianImv;
		 ImageView IcontejiaImv;
		 ImageView IconshijiaImv;
		
		 TextView dsDisheLy;
		 TextView yuanTv;
	}
	
	public class AdapterBillurgeTimerTask extends AsyncTask<Integer, Void,Void>{

		@Override
		protected Void doInBackground(Integer... params) {
			if(AdapterCuiCaiTimer>0 && AdapterCuiCaiTimer<30){
				--AdapterCuiCaiTimer;
				
				if(AdapterCuiCaiTimer==0){
					AdapterCuiCaiTimer = 30;
				}
			}
			Log.i("AdapterCuiCaiTimer", String.valueOf(AdapterCuiCaiTimer));
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			cancel(true);
		}
	}
	
	//菜肴催菜的异步任务
	public class DishurgeTask extends AsyncTask<Dish, Void, Void>{

		@Override
		protected Void doInBackground(Dish... params) {
			BillService.urgedish(params[0].getDinerBillDisheId(), params[0].getIsSet());
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			((CartActivity)(context)).Toast("已为您成功催菜");
			cancel(true);
		}
	}
	
	//删除菜肴的方法
	public void deleteDish(){

	}
}